'use strict';

const BaseController = require("./base");

class UserController extends BaseController {

    async addUser() {
        let { idType } = this.ctx.helper;
        let { uId } = this.ctx.request.query;
        let oneUser = await this.ctx.service.user.findOneUser(idType.wxId, uId);
        if (oneUser) {
            this.faile("用户已存在", -1000);
        } else {
            let userRes = await this.ctx.service.user.addUser({ wxId: uId });
            let accountRes = await this.ctx.service.account.addAccount({ wxId: uId });
            if (userRes && accountRes) {
                this.success(userRes);
            } else {
                this.faile("用户添加失败", -1003);
            }
        }
    }

    async findUser() {
        let allUser = await this.ctx.service.user.findUser();
        this.success(allUser);
    }

    async findOneUser() {
        let { idType } = this.ctx.helper;
        let uId = this.ctx.request.query.uId;
        if (!uId) {
            this.faile("用户wxId错误", -1001);
            return;
        }
        let userResult = await this.service.user.findOneUser(idType.wxId, uId);
        if (userResult) {
            this.success(userResult);
        } else {
            this.faile("暂无用户", -1002);
        }
    }
}

module.exports = UserController;
